#! /bin/sh
prefix="@prefix@"
exec_prefix="@exec_prefix@"
BINDIR="@bindir@"
LINK="@CXX@"
LIBDIR="@libdir@"
LIBS="@dependentlibs@"
CFLAGS="@polyc_CFLAGS@"

DEFAULT_COMPILER="${BINDIR}/poly"
COMPILER="${DEFAULT_COMPILER}"

# Extra options for Windows.  config.status sets these conditionals to either "" or "#".

@NATIVE_WINDOWS_FALSE@EXTRALDFLAGS=""
@NATIVE_WINDOWS_TRUE@@ARCHX86_64_TRUE@EXTRALDFLAGS="-Wl,-u,WinMain"
@NATIVE_WINDOWS_TRUE@@ARCHINTERPRET64_TRUE@EXTRALDFLAGS="-Wl,-u,WinMain"
@NATIVE_WINDOWS_TRUE@@ARCHI386_TRUE@EXTRALDFLAGS="-Wl,-u,_WinMain@16 -Wl,--large-address-aware"
@NATIVE_WINDOWS_TRUE@@ARCHINTERPRET_TRUE@EXTRALDFLAGS="-Wl,-u,_WinMain@16 -Wl,--large-address-aware"
@NATIVE_WINDOWS_TRUE@@WINDOWSGUI_TRUE@EXTRALDFLAGS+=" -mwindows"
@NATIVE_WINDOWS_TRUE@@WINDOWSGUI_FALSE@EXTRALDFLAGS+=" -mconsole"

@NATIVE_WINDOWS_TRUE@SUFFIX="obj"
@NATIVE_WINDOWS_FALSE@SUFFIX="o"

# Msys passes the Windows TEMP in temp (lower case)
# On other systems allow TMPDIR to override /tmp.
@NATIVE_WINDOWS_TRUE@TEMPORARYDIR="${temp:-/tmp}"
@NATIVE_WINDOWS_FALSE@TEMPORARYDIR="${TMPDIR:-/tmp}"

# Extra options for Mac OS X
@MACOSLDOPTS_TRUE@EXTRALDFLAGS="-Wl,-no_pie"

TMPOBJFILE="${TEMPORARYDIR}/polyobj.$$.$SUFFIX"
trap 'rm -f "$TMPOBJFILE"' 0

compile()
{
    echo "val () = use (List.nth(CommandLine.arguments(), 2)); val () = PolyML.export(List.nth(CommandLine.arguments(), 3), main);" | "${COMPILER}" -q --error-exit  "$1" "$2"
}

link()
{
    if [ X"$2" = "X" ]
    then
        "${LINK}" ${EXTRALDFLAGS} ${CFLAGS} "$1" "-L${LIBDIR}" "-Wl,-rpath,${LIBDIR}" -lpolymain -lpolyml ${LIBS}
    else
        "${LINK}" ${EXTRALDFLAGS} ${CFLAGS} "$1" -o "$2" "-L${LIBDIR}" "-Wl,-rpath,${LIBDIR}" -lpolymain -lpolyml ${LIBS}
    fi
}

printhelp()
{
    echo "Usage: polyc [OPTION]... [SOURCEFILE]"
    echo Compile and link a Standard ML source file with Poly/ML.
    echo
    echo "   -b poly      Use 'poly' as compiler instead of ${DEFAULT_COMPILER}"
    echo "   -c           Compile but do not link.  The object file is written to the source file with .$SUFFIX extension."
    echo "   -o output    Write the executable file to 'output'"
    echo "   --help       Write this text and exit"
    exit
}

usage()
{
    echo "$1"
    echo "Usage: polyc [OPTION]... [SOURCEFILE]"
    exit 1
}

checkml()
{
    extension="${1##*.}"
    case "$extension" in
        sml|ML)
             return 0 ;;
        o|obj)
             return 1;;
        *)
             test -r "$1" && file -b "$1" | grep -q text ;;
    esac
}

sourcefile=""
outputfile=""
compileonly="no"

while [ $# -gt 0 ]
do
    case "$1" in
        --help)
            printhelp ;;
        -b)
            shift
            [ $# -eq 0 ] && usage "Expected file name after -b"
            COMPILER="$1";;
        -c) compileonly="yes";;
        -o)
            shift
            [ $# -eq 0 ] && usage "Expected file name after -o"
            outputfile="$1";;
        *)
            [ X"$sourcefile" = "X" ] || usage "Only one source file name allowed"
            sourcefile="$1";;
    esac
    shift
done

[ X"$sourcefile" = "X" ] && usage "No input files"
[ -r "$sourcefile" ] || usage "Error: $sourcefile: No such file"

case "$compileonly" in
     yes)
	 if [ "x$outputfile" = "x" ]; then
	     basename="${sourcefile##*/}"
             outputfile="${basename%.*}.o"
	 fi
         compile "$sourcefile" "$outputfile"
         ;;
     no)
         if checkml "$sourcefile"
         then
             compile "$sourcefile" "$TMPOBJFILE" && link "$TMPOBJFILE" "$outputfile"
         else
             link "$sourcefile" "$outputfile"
         fi
         ;;
esac
